package com.example.mapper;

import com.example.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    /**
     * 查询所有用户
     * @return
     */
    @Select("select * from user_info")
    List<UserInfo> queryAllUser();//接口中的方法默认是抽象方法，省略public abstract

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Select("select * from user_info where id = #{id}")
    List<UserInfo> queryUserById(Integer id);

    /**
     * 根据id和删除标识查询用户
     * 此时有sql中有两个变量，这个时候占位符{}中的名称要和方法行参名一致
     *
     * @param Id
     * @param delete_flag
     * @return
     */
    /*@Select("select * from user_info where id = #{id} and delete_flag = #{delete_flag}")
    List<UserInfo> queryUserByIdAndDeleteFlag(@Param("id") Integer Id, Integer delete_flag);*/
    @Select("select * from user_info where id = #{param1} and delete_flag = #{param2}")
    List<UserInfo> queryUserByIdAndDeleteFlag(@Param("id") Integer Id, Integer delete_flag);

    /**
     * 测试sql结果返回一条数据时是否可以使用对象接收
     * @param id
     * @return
     */
    @Select("select * from  user_info where id = #{id}")
    UserInfo queryById(Integer id);
    /**
     * 测试sql结果返回多条数据时是否可以使用对象接收
     * @param id
     * @return
     */
    @Select("select * from  user_info where id > #{id}")
    List<UserInfo> queryById1(Integer id);
    /**
     * 增加用户
     */
    @Insert("insert into user_info(username,password,age,gender)" +
            " values (#{username},#{password},#{age},#{gender})")
    Integer insertUser(UserInfo userInfo);

    /**
     * 测试方法行参是对象时的使用
     * @param userInfo
     * @return
     */
    @Insert("insert into user_info(username,password,age,gender) "+
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insertUser1(@Param("userInfo") UserInfo userInfo);


    /**
     * 增加用户
     */
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info(username,password,age,gender) "+
            "values (#{username},#{password},#{age},#{gender})")
    Integer insertUser2(UserInfo userInfo);

    /*
     * 删除指定id的用户
     * @param id
     */
    @Delete("delete from user_info where id = #{id}")
    Integer deleteById(Integer id);

    /**
     * 更新用户信息
     * @param userInfo
     * @return
     */
    @Update("update user_info set username = #{username} where id = #{id}")
    Integer update(UserInfo userInfo);

    /**
     * 起别名来实现查询操作
     * @return
     */
    @Select("select id,username,password,age,gender,phone,delete_flag as deleteflag,create_time as createtime" +
            ",update_time as updatetime from user_info")
    List<UserInfo> select();


    /**
     * 结果映射方式
     * @return
     */
    @Results(id = "resultmap",value ={
            @Result(column = "delete_flag",property = "deleteflag"),
            @Result(column = "create_time",property = "createtime"),
            @Result(column = "update_time",property = "updatetime")
    })
    @Select("select * from user_info")
    List<UserInfo> select1();

    @ResultMap("resultmap")
    @Select("select * from user_info where id = #{id}")
    UserInfo select2(Integer id);


    @Select("select * from user_info where id = #{id}")
    UserInfo select3(Integer id);









}
